package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.SysAreaBo;
import com.ruoyi.system.domain.vo.SysAreaVo;
import com.ruoyi.system.service.ISysAreaService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;

/**
 * 城市行政区划
 *
 * @author ruoyi
 * @date 2023-11-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/area")
public class SysAreaController extends BaseController {

    private final ISysAreaService iSysAreaService;

    /**
     * 查询城市行政区划列表
     */
    @SaCheckPermission("system:area:list")
    @GetMapping("/list")
    public TableDataInfo<SysAreaVo> list(SysAreaBo bo, PageQuery pageQuery) {
        return iSysAreaService.queryPageList(bo, pageQuery);
    }
    
    @SaCheckPermission("system:area:tree")
    @GetMapping("/listTree")
    public R<List<SysAreaVo>> listTree(SysAreaBo bo) {
    	List<SysAreaVo> list = iSysAreaService.queryList(bo);
    	return R.ok(list); 
    }

    /**
     * 导出城市行政区划列表
     */
    @SaCheckPermission("system:area:export")
    @Log(title = "城市行政区划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysAreaBo bo, HttpServletResponse response) {
        List<SysAreaVo> list = iSysAreaService.queryList(bo);
        ExcelUtil.exportExcel(list, "城市行政区划", SysAreaVo.class, response);
    }

    /**
     * 获取城市行政区划详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:area:query")
    @GetMapping("/{id}")
    public R<SysAreaVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iSysAreaService.queryById(id));
    }

    /**
     * 新增城市行政区划
     */
    @SaCheckPermission("system:area:add")
    @Log(title = "城市行政区划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAreaBo bo) {
        return toAjax(iSysAreaService.insertByBo(bo));
    }

    /**
     * 修改城市行政区划
     */
    @SaCheckPermission("system:area:edit")
    @Log(title = "城市行政区划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAreaBo bo) {
        return toAjax(iSysAreaService.updateByBo(bo));
    }

    /**
     * 删除城市行政区划
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:area:remove")
    @Log(title = "城市行政区划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysAreaService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
